home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Cafe 3
/
Visual Cafe 3.ISO
/
Vcafe
/
Main.bin
/
FileSystem.java
< prev
next >
Wrap
Text File
|
1998-10-30
|
12KB
|
500 lines
package com.symantec.itools.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;
import com.symantec.itools.lang.IllegalInstantiationError;
import com.symantec.itools.lang.AccessiblePlatform;
import com.symantec.itools.lang.Platform;
import com.symantec.itools.lang.StringUtils;
import com.symantec.itools.util.Properties;
/**
* @author Symantec Internet Tools Division
* @version 1.0
* @since VCafe 3.0
*/
public class FileSystem
{
/**
* @since VCafe 3.0
*/
protected static boolean isCaseSensitive;
// load the properties file
static
{
try
{
Properties properties;
properties = new Properties("/com/symantec/itools/io/FileSystem.properties");
isCaseSensitive = lookupCaseSensitive(properties.getStringList("casesensitive.true"));
}
catch(Throwable ex)
{
ex.printStackTrace();
}
}
protected FileSystem()
{
throw new IllegalInstantiationError(FileSystem.class);
}
/**
* @since VCafe 3.0
*/
public static boolean isCaseSensitive()
{
return (isCaseSensitive);
}
/**
* @param fileName TODO
* @param keepCase TODO
* @since VCafe 3.0
*/
public static String getCanonicalPath(String fileName, boolean keepCase)
{
return (getCanonicalPath(new File(fileName), keepCase));
}
/**
* @param fileName TODO
* @param keepCase TODO
* @param isDir TODO
* @since VCafe 3.0
*/
public static String getCanonicalPath(String fileName, boolean keepCase, boolean isDir)
{
return (getCanonicalPath(new File(fileName), keepCase, isDir));
}
/**
* @param file TODO
* @param keepCase TODO
* @since VCafe 3.0
*/
public static String getCanonicalPath(File file, boolean keepCase)
{
return (getCanonicalPath(file, keepCase, true));
}
/**
* @param file TODO
* @param keepCase TODO
* @param isDir TODO
* @since VCafe 3.0
*/
public static String getCanonicalPath(File file, boolean keepCase, boolean isDir)
{
String fileName;
boolean ensureSeparator;
try
{
String path;
path = file.getPath();
if(path.startsWith("\""))
{
path = path.substring(1);
}
if(path.endsWith("\""))
{
path = path.substring(0, path.length() - 1);
}
fileName = new File(path).getCanonicalPath();
}
catch(IOException ex)
{
fileName = file.getAbsolutePath();
}
if(!(isCaseSensitive))
{
if(Platform.isOSType(Platform.OS_TYPE_WINDOWS) && keepCase)
{
String dir;
dir = fileName.substring(0, 1).toLowerCase();
fileName = dir + fileName.substring(1, fileName.length());
}
else
{
fileName = fileName.toLowerCase();
}
}
ensureSeparator = false;
if(!(file.exists()))
{
if(isDir)
{
ensureSeparator= true;
}
}
else if(file.isDirectory() && !(fileName.endsWith(File.separator)))
{
ensureSeparator = true;
}
if(ensureSeparator)
{
fileName += File.separator;
}
return (fileName);
}
/**
* @since VCafe 3.0
*/
public static String getCurrentDirectory()
{
return (getCanonicalPath(new File("."), true));
}
/**
* @param list TODO
* @since VCafe 3.0
*/
protected static boolean lookupCaseSensitive(String[] list)
{
String osType;
osType = AccessiblePlatform.getOSType();
if(osType == null)
{
// assume case sensitive
return (true);
}
for(int i = 0; i < list.length; i++)
{
if(osType.equals(list[i]))
{
return (true);
}
}
return (false);
}
/**
* @param path TODO
* @since VCafe 3.0
*/
public static String[] getPath(String path)
{
return (getPath(path, false));
}
/**
* @param path TODO
* @param allowFiles TODO
* @since VCafe 3.0
*/
public static String[] getPath(String path, boolean allowFiles)
{
StringTokenizer tokenizer;
String[] actualPath;
Vector list;
tokenizer = new StringTokenizer(path, File.pathSeparator);
list = new Vector();
while(tokenizer.hasMoreTokens())
{
String entry;
entry = FileSystem.getCanonicalPath(tokenizer.nextToken(), true);
if(!(list.contains(entry)))
{
File file;
file = new File(entry);
// is it on the disk?
if(file.exists())
{
// is it a directory or are we allowing files?
if(allowFiles || !(allowFiles) && file.isDirectory())
{
list.addElement(entry);
}
}
// doesn't exist so doesn't matter if it is file.
else
{
list.addElement(entry);
}
}
}
actualPath = new String[list.size()];
list.copyInto(actualPath);
return (actualPath);
}
/**
* @param path TODO
* @since VCafe 3.0
*/
public static String[] getValidPath(String path)
{
return (getValidPath(path, false));
}
/**
* @param path TODO
* @param allowFiles TODO
* @since VCafe 3.0
*/
public static String[] getValidPath(String path, boolean allowFiles)
{
StringTokenizer tokenizer;
String[] validPath;
Vector list;
tokenizer = new StringTokenizer(path, File.pathSeparator);
list = new Vector();
while(tokenizer.hasMoreTokens())
{
String entry;
entry = FileSystem.getCanonicalPath(tokenizer.nextToken(), true);
if(!(list.contains(entry)))
{
File file;
file = new File(entry);
// is it on the disk?
if(file.exists())
{
// is it a directory or are we allowing files?
if(allowFiles || !(allowFiles) && file.isDirectory())
{
list.addElement(entry);
}
}
}
}
validPath = new String[list.size()];
list.copyInto(validPath);
return (validPath);
}
/**
* @param name1 TODO
* @param name2 TODO
* @since VCafe 3.0
*/
public static boolean compareFilenames(String name1, String name2)
{
if(isCaseSensitive)
{
return (name1.equals(name2));
}
return (name1.equalsIgnoreCase(name2));
}
public static boolean compareFilenames(File file1, File file2)
{
return (compareFilenames(getCanonicalPath(file1, true),
getCanonicalPath(file2, true)));
}
/**
* @param fileName TODO
* @param directory TODO
* @since VCafe 3.0
*/
public static boolean isInPath(String fileName, String directory)
{
String dir;
fileName = FileSystem.getCanonicalPath(fileName, true);
dir = fileName.substring(0, fileName.lastIndexOf(File.separatorChar)+1);
if(isCaseSensitive)
{
return (dir.startsWith(directory));
}
return (StringUtils.startsWithIgnoreCase(dir, directory));
}
/**
* @param fileName TODO
* @since VCafe 3.0
*/
public static String quoteIfNeeded(String fileName)
{
// NOTE: is this OS specific? If so we need to do this different!
// does it have a space
if(fileName.indexOf(' ') != -1)
{
// make sure that we wrap the filename in spaces
if(!(fileName.startsWith("\"")))
{
fileName = "\"" + fileName;
}
if(!(fileName.endsWith("\"")))
{
fileName += "\"";
}
}
else
{
// make sure the file name has no spaces
if(fileName.startsWith("\""))
{
fileName = fileName.substring(1);
}
if(fileName.endsWith("\""))
{
fileName = fileName.substring(0, fileName.length() - 1);
}
}
return (fileName);
}
/**
* @param fileName TODO
* @since VCafe 3.0
*/
public static String quote(String fileName)
{
// NOTE: is this OS specific? If so we need to do this different!
// make sure that we wrap the filename in spaces
if(!(fileName.startsWith("\"")))
{
fileName = "\"" + fileName;
}
if(!(fileName.endsWith("\"")))
{
fileName += "\"";
}
return (fileName);
}
public static boolean copyFile(File source, File target)
throws IOException
{
BufferedInputStream streamIn;
BufferedOutputStream streamOut;
File targetDir;
if(!(source.exists()))
{
throw new IOException(getCanonicalPath(source, true) + " does not exist");
}
targetDir = new File(target.getParent());
// don't bother copying over the same file!
if(compareFilenames(source, target))
{
return (true);
}
if(!(targetDir.exists()))
{
if(!(targetDir.mkdirs()))
{
throw new IOException("Can't create " + target.getParent());
}
}
streamIn = null;
streamOut = null;
try
{
byte[] buf;
int count;
streamIn = new BufferedInputStream(new FileInputStream(source));
streamOut = new BufferedOutputStream(new FileOutputStream(target));
buf = new byte[1024];
while((count = streamIn.read(buf)) > 0)
{
streamOut.write(buf, 0, count);
}
}
finally
{
if(streamIn != null)
{
try
{
streamIn.close();
}
catch(IOException ex)
{
}
}
if(streamOut != null)
{
try
{
streamOut.close();
}
catch(IOException ex)
{
}
}
}
return (true);
}
}